﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace MovieDB.Common
{
    public static class EnumToSelectItemConversion
    {
        public static IEnumerable<SelectListItem> GetSelectListItem<T>(Enum itemToSelect = null) where T : struct
        {
             var result= Enum.GetValues(typeof (T))
                 .Cast<Enum>()
                 .Where(enumValue => ((int)(object)enumValue) != -1)
                 .Select(enumValue => new SelectListItem
                                         {
                                             Text = enumValue.GetEnumDescription(),
                                             Value = enumValue.ToString(),
                                             Selected = (itemToSelect == null) ? false : (enumValue.Equals(itemToSelect))
                                         });

            return result;
        }
    }
}